const { ErrorModel, SuccessModel } = require('../utils/index')
const logger = require('../utils/logger')
const { DataProjectModel } = require('../models/project');
const { DataPageModel } = require('../models/page');
const { generatePageUrl } = require("../utils/index");
const { eventEmitter } = require('../utils/copyFile');
const { savePageFile } = require("./../config/index");
class ProjectController {
  /**
   * @description 添加项目
   * @param {*} name
   * @param {*} thumb
   * @param {*} remark
   */
  static async projectAdd (req, res, next) {
    const { name, thumb, remark } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`ProjectController`, `projectAdd`, `req.body`, req.body)
    if (!name) {
      res.json(new ErrorModel([], `请输入项目名称`))
      return
    }
    try {
      const projectInfo = await DataProjectModel.findByProjectName(name)
      if (projectInfo) {
        res.json(new ErrorModel([], `项目已存在`))
        return
      }
      const result = await DataProjectModel.create(name, thumb, remark, op_user_id)
      logger.info(`ProjectController`, `projectAdd`, `result`, result)
      res.json(new SuccessModel([], `新增项目成功`))
    } catch (error) {
      logger.error(`ProjectController`, `projectAdd`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 更新项目
   * @param {*} project_id
   * @param {*} name
   * @param {*} thumb
   * @param {*} remark
   */
  static async projectUpd (req, res, next) {
    const { project_id, name, thumb, remark, publish } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`ProjectController`, `projectUpd`, `req.body`, req.body)
    if (!project_id) {
      res.json(new ErrorModel([], `请输入必填项`))
      return
    }
    try {
      const projectInfo = await DataProjectModel.findByProjectNameNeId(name, project_id)
      if (projectInfo) {
        res.json(new ErrorModel([], `项目已存在`))
        return
      }
      const result = await DataProjectModel.update(name, thumb, remark, publish, op_user_id, project_id)
      logger.info(`ProjectController`, `projectUpd`, `result`, result)
      res.json(new SuccessModel([], `项目更新成功`))
    } catch (error) {
      logger.error(`ProjectController`, `projectUpd`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 删除项目
   * @param {*} project_id
   */
  static async projectDel (req, res, next) {
    const { project_id } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`ProjectController`, `projectDel`, `req.body`, req.body)
    if (!project_id) {
      res.json(new ErrorModel([], `project_id 获取失败`))
      return
    }
    try {
      const result = await DataProjectModel.delete(op_user_id, project_id)
      logger.info(`ProjectController`, `projectDel`, `result`, result)
      res.json(new SuccessModel([], `项目删除成功`))
    } catch (error) {
      logger.error(`ProjectController`, `projectDel`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }

  /**
   * @description 项目分页列表
   * @param {*} limit
   * @param {*} size
   * @param {*} name
   * @param {*} remark
   */
  static async projectList (req, res, next) {
    logger.info(`ProjectController`, `projectList`, `req.query`, req.query)
    try {
      const { result, count } = await DataProjectModel.list()
      logger.info(`ProjectController`, `projectList`, `result`, result, count)
      res.json(new SuccessModel({ result }, `获取项目数据成功`))
    } catch (error) {
      logger.error(`ProjectController`, `projectList`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
  /**
   * @description 上传PNG文件
   */
  static async dataPNGUpload (req, res, next) {
    const image = req.file.path.substring(6,);
    res.json(new SuccessModel({ path: image }, `上传成功`))
  }
  /**
   * @description 拷贝项目
   * @param {*} project_id
   */
  static async projectCopy (req, res, next) {
    const { project_id } = req.body
    const { user_id: op_user_id } = req.user
    logger.info(`ProjectController`, `projectCopy`, `req.body`, req.body)
    if (!project_id) {
      res.json(new ErrorModel([], `project_id 获取失败`))
      return
    }
    try {
      const projectResult = await DataProjectModel.findByProjectId(project_id)
      if (projectResult.name) {
        const { name, thumb, remark } = projectResult
        const projectAddResult = await DataProjectModel.create(name, thumb, remark, op_user_id)
        logger.info(`ProjectController`, `projectCopy`, `projectAddResult`, projectAddResult)
        const addPages = await DataPageModel.copyByProjectId(project_id, projectAddResult.project_id)
        logger.info(`ProjectController`, `projectCopy`, `addPages`, addPages)
        const pageResult = await DataPageModel.findByProjectId(projectAddResult.project_id)
        if (!pageResult.length) {
          res.json(new SuccessModel([], '拷贝成功'))
        } else {
          pageResult.map(async item => {
            let page_url = generatePageUrl(item.project_id, item.page_id)
            eventEmitter.emit("copyFile", `${savePageFile}${item.page_url}.json`, `${savePageFile}${page_url}.json`)
            await DataPageModel.updatePageURL(item.page_id, page_url)
          })
        }
        res.json(new SuccessModel([], '拷贝成功'))
      } else {
        logger.error(`ProjectController`, `projectCopy`, `error`, error)
        res.json(new ErrorModel([], error.message))
      }
    } catch (error) {
      logger.error(`ProjectController`, `projectCopy`, `error`, error)
      res.json(new ErrorModel([], error.message))
    }
  }
}

module.exports = {
  ProjectController,
}
